package com.example.spring_native_es;

import co.elastic.clients.elasticsearch.ElasticsearchClient;
import co.elastic.clients.elasticsearch._types.query_dsl.Query;
import co.elastic.clients.elasticsearch._types.query_dsl.QueryBuilders;
import co.elastic.clients.elasticsearch.core.*;
import co.elastic.clients.elasticsearch.core.search.Hit;
import co.elastic.clients.elasticsearch.core.search.HitsMetadata;
import co.elastic.clients.elasticsearch.core.search.SourceConfig;
import co.elastic.clients.elasticsearch.core.search.SourceConfigParam;
import co.elastic.clients.elasticsearch.indices.CreateIndexResponse;
import com.example.spring_native_es.model.Product;
import org.assertj.core.util.Lists;
import org.junit.jupiter.api.Test;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.io.IOException;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 *
 */
@SpringBootTest
public class QueryEs {
    private static final String INDEX_NAME = "product";
    @Autowired
    private ElasticsearchClient elasticsearchClient;

    @Test
    public void test_create_index() throws IOException {
        CreateIndexResponse createIndexResponse = elasticsearchClient.indices().create(e ->
                e.index("product")
        );

        System.out.println(createIndexResponse);
    }

    @Test
    public void test_insert() throws IOException {
        // 配置日志记录器
        ch.qos.logback.classic.Logger log =
                (ch.qos.logback.classic.Logger) LoggerFactory.getLogger("co.elastic.clients.transport");
        log.setLevel(ch.qos.logback.classic.Level.DEBUG);
        CreateRequest<Product> build = new CreateRequest.Builder<Product>()
                .index(INDEX_NAME)
                .id("40")
                .document(new Product(1L, "tom", BigDecimal.valueOf(1.2345),
                        new Date(),
                        new Date()
                )).build();
        CreateResponse createResponse = elasticsearchClient.create(build);
        System.out.println(createResponse);
    }


    @Test
    public void test_query() throws IOException {
        long l = System.currentTimeMillis();
        Query q = new Query(QueryBuilders.matchAll().build());
        SearchResponse<Product> product = elasticsearchClient.search(SearchRequest.of(e ->
                e.index(INDEX_NAME).query(q)), Product.class);

        HitsMetadata<Product> hits = product.hits();
        List<Hit<Product>> hits1 = hits.hits();
        Product source = hits1.get(0).source();
        System.out.println(source);
        System.out.println(product);

        System.out.println("cost " + (System.currentTimeMillis() - l));
    }


    @Test
    public void test_get() throws IOException {
        GetRequest request = new GetRequest.Builder()
                .index(INDEX_NAME)
                .id("44")
                .sourceIncludes(Lists.newArrayList("name", "price"))
                .build();
        System.out.println(request);
        GetResponse<Product> productGetResponse = elasticsearchClient.get(request, Product.class);
        System.out.println(productGetResponse);
    }
}
